package project.active;

import java.util.ArrayList;

import model.pojo.Employee;
import project.dao.EmployeeDAO;
import project.dao.PrCompositionDAO;
import project.dao.ProjectDAO;
import project.model.PrProject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import approve.action.R;

public class Lvtesterlist extends Activity {
	Bundle language;
	String lang="EN",database="admin";
	PrProject pr =new PrProject();
	ProjectDAO prodao=new ProjectDAO();
	EmployeeDAO emdao=new EmployeeDAO();
	Employee emp=new Employee();
	PrCompositionDAO pcdao=new PrCompositionDAO();
	TextView logout,username;
	ListView lvtesterlist;
	TextView proinfo,title;
	Button btUpdate,btGoSub,btDelete;
	
	String[] tt=null;
	ArrayList<Employee> testerlst=new ArrayList<Employee>();
	Lpopupcoder adtest;
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lvtesterlist);
		language = getIntent().getExtras().getBundle(getLang());
		
		title=(TextView)findViewById(R.id.l_tv_title_testlist);
		title.setText(language.getString("tester_team"));
		lvtesterlist=(ListView)findViewById(R.id.l_lv_TestList);
		proinfo=(TextView)findViewById(R.id.l_tv_TestListProInfo);
		btUpdate=(Button)findViewById(R.id.lbt_goUpTestList);
		btGoSub=(Button)findViewById(R.id.lbt_TestListToSubMenu);
		btGoSub.setText(language.getString("project_info"));
		btDelete=(Button)findViewById(R.id.lbt_TestListDelete);
		btDelete.setText(language.getString("Delete"));
		btUpdate.setText(language.getString("ac_du_addmore"));
		
		Intent i=this.getIntent();
		pr=(PrProject)i.getSerializableExtra("project");		
		testerlst=pr.getListTesterEm(getDatabase());
		emp=(Employee)i.getSerializableExtra("em");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		
		proinfo.setText(pr.getIdProject()+" - "+pr.getNameProject());		
		if(testerlst!=null && testerlst.size()>0){
			//lvtesterlist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, testerlst));
			adtest = new Lpopupcoder(this, testerlst);
			lvtesterlist.setAdapter(adtest);
			btDelete.setEnabled(true);
		}else
			btDelete.setEnabled(false);
		
		btGoSub.setOnClickListener(goSubMenu);
		btUpdate.setOnClickListener(gotoUpTesterList);
		btDelete.setOnClickListener(showdialogDelete);
		
	}
	
	private OnClickListener showdialogDelete = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			tt=adtest.getIdCheck();
			if(tt!=null && tt.length>0){
				
				openDialogDelete(tt);
			}
			
		}
	};
	
	private void openDialogDelete(final String[] tt) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃªu Ä‘á»�
		builder.setMessage(language.getString("ac_tb_Delete"));
		builder.setCancelable(true);// Set cÃ³ cho ngÆ°á»�i dÃ¹ng Cancer báº±ng nÃºt
										// quay láº¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃ m cÃ¡i gÃ¬ Ä‘Ã³ khi áº¥n Yes táº¡i Ä‘Ã¢y
				
				deletelistcoder(tt);
				dialog.dismiss();
				
			}
		});
		
		builder.setNegativeButton(language.getString("cancel"), null);

		builder.show();
	}
	
	public void deletelistcoder(String[] lst){
		if(lst!=null && lst.length>0){
			String s="";
			for(int i=0;i<lst.length;i++){
				if(lst[i]!=null){
					if(s.equals("")){
						s="'"+lst[i]+"'";
					}else{
						s+=",'"+lst[i]+"'";
					}
				}
				
			}
			
			if(!s.equals("")){
				if(pcdao.deletePosTest(getDatabase(), s, pr.getIdProject())>0){
					Intent i= new Intent(Lvtesterlist.this,Lvtesterlist.class);
					i.putExtras(getIntent().getExtras());
					i.putExtra("lang", getLang());
					i.putExtra("database", getDatabase());
					i.putExtra("em",emp);
					i.putExtra("project", pr);
					startActivity(i);
				}else{
					openDialogXoaKhongDuoc();
				}
			}
		}
	}
	
	private void openDialogXoaKhongDuoc() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(language.getString("infoSys"));// Set tiÃªu Ä‘á»�
		builder.setMessage(language.getString("ac_DeleteFail"));
		builder.setCancelable(false);// Set cÃ³ cho ngÆ°á»�i dÃ¹ng Cancer báº±ng nÃºt
										// quay láº¡i (back) ko? false: ko
		builder.setPositiveButton(language.getString("yes"), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃ m cÃ¡i gÃ¬ Ä‘Ã³ khi áº¥n Yes táº¡i Ä‘Ã¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
	
	private OnClickListener goSubMenu = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lvtesterlist.this, LSubprojectlistmenu.class);
			i.putExtras(getIntent().getExtras());
			i.putExtra("em",emp);
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("project", pr);
			
			startActivity(i);
		}
	};
	
	private OnClickListener gotoUpTesterList = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i= new Intent(Lvtesterlist.this,Lupdatetesterlist.class);
			i.putExtras(getIntent().getExtras());
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("em",emp);
			i.putExtra("project", pr);
			
			startActivity(i);
		}
	};
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lvtesterlist.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
